<template>
  <div class="btns">
    <Button v-auth="'waitEmployOffer'" color="primary" @click="sendOffer">录取</Button>
    <Button v-auth="'waitEmployEliminate'" color="#E85B5B" @click="dieOut">淘汰</Button>
  </div>
  <BasicModal :show="show" :title="title" width="30%" :widthComfirm="true" @comfirm="comfirm"
    @closeModal="show = false">
    <template #content>
      <CommonForm :formList="formList" ref="formRef" labelWidth="150px"></CommonForm>
    </template>
  </BasicModal>

</template>

<script setup lang='ts'>
import { ref, watch } from "vue"
import { Button } from "@/components/Button";
import BasicModal from "@/views/ApplicationDetails/Modals/BasicModal.vue";
import type { IndexFormItem } from "@/views/ApplicationDetails/application-type.ts";
import CommonForm from "@/views/applicationDetails/CommonForm.vue";
import { message } from "ant-design-vue";
import { offerIssued, getEntrySchoolInfo, selectCommonConfigFoChildren, selectTrainingNumberFoTime, progressIndex } from "@/api/application-details/index.ts";
import { comStores } from '@/store/common/comStore';

const store = comStores()
interface Options {
  label: string;
  value?: string | number
}
// 
const title = ref<string>("录取")
const show = ref<boolean>(false)
const formRef = ref<any>(null);
const formList = ref<IndexFormItem>([
])
watch(() => show.value, (val) => {
  if (val) {
    getOptions()
  }
})

const getOptions = async () => {
  const subject = await selectCommonConfigFoChildren({ type: 1 }) //科目
  formList.value[2].options = subject.data;
}
const getData = async () => {
  const { resumeId } = store.currentResume
  const res = await progressIndex({ type: 0, resumeId });
  if (res && res.code === '10000') {
    console.log(res.data, 'res.data')
    const { trainingId } = res.data;
    const data = await selectTrainingNumberFoTime({ trainingNumberId: trainingId });
    console.log(data, '===>data')
    if (data && data.code == '10000') {
      return data.data
    } else {
      message.error(data.msg)
    }
  }
}
const sendOffer = async () => {
  const dataInfo = await getData();
  show.value = true
  title.value = '录取';
  formList.value = [
    { label: "录取结果通知时间", name: "offerSendTime", bindMethods: (val) => { return timeChange(val) }, value: dataInfo.resultNoticeTime, component: 'datePicker', format: "YYYY-MM-DD HH:mm", disabledDate: true, showTime: true, minuteStep: 5 },
    { label: "领取offer时间", name: "receiveTime", useDisabledFlag: "offerSendTime", value: "", component: 'datePicker', format: "YYYY-MM-DD HH:mm", disabledDate: true, showTime: true, minuteStep: 5 },
    { label: "录取", name: "cascader", value: "", component: 'cascader', options: [] },
    { label: "预计到岗时间", name: "onDutyTime", value: "", component: 'datePicker', required: false, format: "YYYY-MM-DD", disabledDate: true },
    { label: "备注", name: "remark", value: "", component: 'textarea', required: false },
    { label: "发送邮件通知", name: "isSend", value: true, component: 'isSend', required: false },
  ]
  formList.value[1].value = dataInfo.receiveTime
  console.log('sendOffer  待录取');
}
const emits = defineEmits(['dieOut']);
const timeChange = (val) => {
  if (val.value) {
    formList.value[1].value = formatTimestamp(new Date(val.value).getTime() + 60 * 60 * 1000)
  }

}
function formatTimestamp(timestamp) {
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');

  return `${year}-${month}-${day} ${hours}:${minutes}`;
}

const dieOut = () => {
  console.log('dieout  待录取');
  show.value = true
  title.value = '淘汰'
  formList.value = [
    { label: "录取结果通知时间", name: "offerSendTime", value: "", component: 'datePicker', format: "YYYY-MM-DD HH:mm", showTime: true, minuteStep: 5 },
    { label: "淘汰原因", name: "reason", value: "", component: 'textarea', required: false },
    { label: "发送邮件通知", name: "isSend", value: true, component: 'isSend', required: false },
  ]
}
const comfirm = async () => {
  const res = await formRef.value.submitForm();
  if (!res) return;
  show.value = false;
  const { resumeId, processId } = store.currentResume;
  const { userName } = store.userInFor
  let param: any = {
    resumeId: resumeId,
    processId: processId,
    operator: userName,
  }
  if (title.value == '录取') {
    param.result = '通过';
    let obj: any = {}
    res.forEach(el => {
      obj[el.name] = Array.isArray(el.value) ? el.value.join(',') : el.value
    })
    obj.cascader = obj.cascader.split(',')
    param.school = obj.cascader[0]
    param.subject = obj.cascader[1];
    param.grade = obj.cascader[2];
    obj.receiveTime = obj.receiveTime + ':00'
    obj.offerSendTime = obj.offerSendTime + ':00'
    obj.onDutyTime = obj.onDutyTime ? obj.onDutyTime + " 00:00:00" : ""
    param = { ...param, ...obj }
  } else {
    param.result = '淘汰';
    param.offerSendTime = res[0].value + ':00'
    param.reason = res[1].value
    param.isSend = res[2].value
  };
  const data = await offerIssued(param);
  if (data && data.code === '10000') {
    message.success("操作成功")
    store.getResumeInfo(resumeId)
  } else {
    message.error(data.msg)
  }
  // formRef.value.resetFields()
}
</script>
<style lang="less" scoped>
.btns {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}
</style>
